/*
 * open_msm2.c
 *
 * Created: 04.01.2012 21:19:49
 *  Author: Luke
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdint.h>
#include "drivers/SPI.h"
#include "drivers/led.h"
#include "drivers/nfamp.h"
#include "drivers/IO.h"
#include "vs1011/vs1011e.h"
#include "sdfat-rr/sdfat-rr.h"
#include "sdfat-rr/sd_raw.h"
#include "sdfat-rr/uart.h"
#include "Playback.h"

void initAll(void)
{
	ioInit();
	ledInit();
	ampInit();
	vsInit();
	spiInit();
	#ifdef DEBUG
		uart_init();
	#endif
	sd_raw_config();
	//finally enable interrupts
	sei();
}

int main(void)
{
	initAll();
	
#ifdef DEBUG
	uart_puts_p(PSTR("    *******        \nStartup...\n"));
#endif

	ledBlinkBlue(1);

#ifdef DEBUG	
	uart_puts_p(PSTR("Startup Buttons: "));
	uart_putw_dec(startup_buttons_);
	uart_putc('\n');
#endif
	
	ledRed();
	//wait for SDcard to become available
	while(!sd_raw_available());
	ledOff();
	
	if (!sdFatInit())
	{
		//that did not go well...
		_delay_ms(500);
		ledBlinkRed(3);
		ioSelfHoldOff();
		while(1);
	}
	
	ioFakeButton(startup_buttons_);
	
#ifdef DEBUG
	uart_puts_p(PSTR("startup song: "));
	uart_putw_dec(pbGetNextSong());
	uart_putc('\n');
#endif
	
	while (pbGetNextSong())
	{
		pbStartPlayback();
	}
	
	ledBlinkBlue(3);
	_delay_ms(1000);
	ioSelfHoldOff();
	while(1);
}
